package org.example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import java.sql.Driver;
import java.util.List;

import static java.lang.Thread.sleep;

public class Main {
    public static void main(String[] args) throws InterruptedException {
    test1();
//    test2();
//    test3();
//        test4();
//        test5();

//        test01();
//        test02();

    }

    //通过id定位
    private static void test01() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        webDriver.findElement(By.id("kw")).sendKeys("java");
    }
    //2.name定位
    private static void test02() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        webDriver.findElement(By.name("wd")).sendKeys("java");
    }

    private static void test5() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        WebElement search_input = webDriver.findElement(By.cssSelector("#kw"));
        
    }

    private static void test4() throws InterruptedException {
        TrueTest();
//        FalseTest();
    }
    //submit 点击和  click点击，submit点击会报错
    private static void FalseTest() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        //找到“新闻”按钮
        WebElement news_button = webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)"));
        news_button.submit();
        webDriver.quit();
    }


    private static void TrueTest() throws InterruptedException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        WebElement news_button= webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)"));
        news_button.click();
        sleep(3000);
    }

    private static void test3() throws InterruptedException {
        WebDriver webDriver =new ChromeDriver();
        webDriver.get("https://www.baidu.com");
        //找到搜索框
        WebElement search_input = webDriver.findElement(By.cssSelector("#kw"));
        //向搜索框输入“软件测试”
        search_input.sendKeys("软件测试");
        sleep(3000);
        //清空搜索框
        search_input.clear();
        //向搜索框输入框中输入“前端”
        search_input.sendKeys("前端");
        //找到“百度一下”，进行点击
        webDriver.findElement(By.cssSelector("#su")).submit();
        //校验结果是不是有前端
        int flag =0;
        sleep(3000);
        List<WebElement> search_results = webDriver.findElements(By.xpath("//font[text()=\"前端\"]"));
        for (int i=0;i<search_results.size();i++){
            if (search_results.get(i).getText().equals("前端")){
                flag = 1;
                break;
            }
        }
        if (flag ==1){
            System.out.printf("测试通过");
        }else {
            System.out.printf("测试不通过");
        }
//        webDriver.quit();
    }

    private static void test2() throws  InterruptedException {
        //create deiver
        WebDriver webDriver = new ChromeDriver();
        //open baidu.com
        webDriver.get("http://www.baidu.com/");
        //find  search_input_field
        WebElement search_input = webDriver.findElement(By.cssSelector("kw"));
        // Type "Software testing" into the serch box
        search_input.sendKeys("软件测试");
        //Find the webset "baidu"
        WebElement baidu_button = webDriver.findElement(By.cssSelector("#su"));
        //click the webset
        baidu_button.click();
        //sleep 3000 seconds
        sleep(3000);
        //find the " elements about software testing" in the page
        List<WebElement> search_results = webDriver.findElements(By.cssSelector("a em"));
        sleep(3000);
        //ergodic List
        for (int i=0;i<search_results.size();i++){
            if (search_results.get(i).getText().equals("软件测试")){
                System.out.printf("测试通过");
            }else{
                System.out.printf("测试不通过");
            }
        }
        webDriver.quit();
    }

    public static void test1() {
        //创建驱动
        WebDriver webDriver = new ChromeDriver();
        //打开百度
        webDriver.get("https://www.baidu.com");
        //定位百度搜索框（通过css选择器定位元素）
        WebElement search_input = webDriver.findElement(By.cssSelector(".s_ipt"));
        //通过xpath定位元素
        WebElement search_input1= webDriver.findElement(By.xpath("//form/span[1]/input"));
        //通过标签定位
        WebElement search_input2 = webDriver.findElement(By.className("input"));
        WebElement search_input3 = webDriver.findElement(By.tagName("input"));
        if (search_input == null){
            System.out.printf("测试不通过");
        }else {
            System.out.printf("测试通过");
        }
        //关闭浏览器
//        webDriver.close();
        webDriver.quit();
    }





}